<?php
namespace Home\Controller;

class UsercenterController extends AdminController{

	private $_userPhoto;//操作相册对像
	private $_userInfo;//操作用户信息的表
    private $_user;//操作用户表
    private $_model; //操作model类
    private $_foucs; //操作 love_foucs类
	public function __construct(){
		parent::__construct();
		$this->_user=M('user');
        $this->_userInfo=M('userinfo');
		$this->_userPhoto=M('userphoto');
		$this->_model=M();
		$this->_foucs=M('foucs');
	}
	public function index(){
		$id=$_SESSION['love_user']['id'];
        var_dump($_SESSION);
         //模拟session
        $sql="select s_city,sex from love_userinfo where uid={$id}";
        $dataCity=$this->_userInfo->query($sql);
        // dump($dataCity);    
        $cityId=(empty($dataCity[0]['s_city'])==1)?6:$dataCity[0]['s_city'];
        $sex=$dataCity[0]['SEX'];
        $tsex=($id=='1')?1:0; 
        // dump($tsex);
        $sql="select u.uid uuid,u.name uname,sex,p.name pname,edu from love_userinfo u,love_userphoto p where u.uid=p.uid and s_city={$cityId} and p.is_face=1 and sex={$tsex} and u.uid in (select kid from love_foucs where uid={$id}) order by rand() limit 5";
        $dataList1=$this->_model->query($sql);
        // dump($this->_model->getLastSql());
        //查询未读消息
        
        //查询已关注的
        $sql1="select count(id) as num from love_foucs where uid={$id}";
        $ygz=$this->_foucs->query($sql1);
        
        //查询积分
   		 $sql2="select value from love_integral where uid={$id}";
         $num=$this->_model->query($sql2);
         	 dump($num);
        //查询被关注的
        $sql3="select count(id) as mum from love_foucs where kid={$id}";
        $bgz=$this->_foucs->query($sql3);

        $this->assign('ygz',$ygz);
        $this->assign('num',$num);
        $this->assign('bgz',$bgz);
        $this->assign('dataList',$dataList1);
		$this->display();
	}
	public function upload(){

		dump(__ROOT__);
		$upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
		$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload();  
        $fileName='D:/wamp/www/object4/object/app/Uploads'.'/'.$info['avatar_file']['savepath'];
        $imgName=$info['avatar_file']['savename'];
        
        import("Org.Util.picture");
        $imgDeal=new \picture($fileName.$imgName);
        $code=$_POST['avatar_data'];
        $code_deal=json_decode($code);
    
        $temp=$code_deal->x;
        $imgDeal->CUT_X=$code_deal->x;
        $imgDeal->CUT_Y=$code_deal->y;
        $imgDeal->CUT_WIDTH=$code_deal->width;
        $imgDeal->CUT_HEIGHT=$code_deal->height;
        $imgDeal->CUT();

         imagejpeg($imgDeal->TRUE_COLOR,$fileName.$temp.$imgName);   ///输出图片 此处待修改
     
         if(!$info) {// 上传错误提示错误信息
             $this->error($upload->getError());
             }else{// 上传成功
        	//将图片信息存入数据库
		       $img['name']=__ROOT__.'/Uploads/'.$info['avatar_file']['savepath'].$temp.$imgName;
		       $img['uid']=I('get.id/d');
		       $img['is_face']=1;
		       /*var_dump($img);*/
		       $uid=$img['uid'];
		       // var_dump($uid);
		       $this->_userPhoto->is_face=0;
		       $this->_userPhoto->where("uid={$uid}")->save();
		       $this->_userPhoto->add($img);
		      }
	}


    public function money(){
        /*var_dump($_SESSION);*/
        $uid = $_SESSION['love_user']['id'];
        $money = $_POST['value'];
        /*var_dump($_POST);
        var_dump($money);*/
        /*exit;*/

        if($_SESSION['dict']==0){
            $x = 100;
        }else{
             $x = $_SESSION['dict'];  
        }
        $integral = $money * $x ;
        $ral['uid'] = $uid;
        /*var_dump($ral['uid']);
        exit;*/
        $ral['type'] = 1; 
        $ral['value'] = $integral;
        $ral['reason'] = '充值';
        $ral['utime'] = time();
        $data = M('userinfo')->where("uid=$uid")->find();
        /*var_dump($data);*/
        /*echo M('userinfo')->getLastSql();*/
        /*exit;*/
        /*var_dump($data['integral']);
        var_dump($integral);
        exit;*/
        $ral1 = $data['integral']+$integral;
        /*echo $ral;*/
        M('userinfo')->integral = $ral1;
        if(M('userinfo')->where("uid=$uid")->save()>0){
            /*echo M('userinfo')->getLastSql();
            exit;*/
            if(M('integral')->add($ral)>0){
               /* echo M('integral')->getLastSql();
                exit;*/
                $this->success('充值成功',U('Usercenter/index'));
            }else{
                /*echo 1;*/
                $this->error('充值失败');
            }
        }else{
            /*echo M('userinfo')->getLastSql();
            exit;
            echo 2;*/
            $this->error('充值失败');
        }

    }
}
